﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Services;

namespace AnesSystem.ViewModels
{
    public class PacuRealTimeSignsConfigCtrlViewModel : BaseViewModel<PacuRealTimeSignsConfigCtrlViewModel>
    {
        #region Variables
        private List<ConfigVitalSignsModel> _allSignsList;
        private Dictionary<string, ConfigVitalSignsModel> _allNameToSign;
        #endregion


        #region Constructor
        public PacuRealTimeSignsConfigCtrlViewModel()
        {
            //                  InitData();
        }



        #endregion


        #region Properties
        public List<ConfigVitalSignsModel> AllSignsList
        {
            get
            {
                return _allSignsList;
            }

            set
            {
                _allSignsList = value;
            }
        }

        public Dictionary<string, ConfigVitalSignsModel> AllNameToSign
        {
            get
            {
                return _allNameToSign;
            }

            set
            {
                _allNameToSign = value;
            }
        }


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private void InitData()
        {
            Clear();
            GetAllSigns();
            DividedShowAndNotShowSigns();
        }

        private void Clear()
        {
            PacuAutoShowSignsCtrlViewModel.Instance.AutoShowSignsList.Clear();
            PacuNoShowSignCtrlViewModel.Instance.NotAutoShowSignsList.Clear();
            PacuAutoShowSignsCtrlViewModel.Instance.TableShowSignList.Clear();
            PacuAutoShowSignsCtrlViewModel.Instance.LineShowSignList.Clear();
        }

        private void GetAllSigns()
        {

            _allSignsList = DataManager.Instance.ConfigVitalSignsModelList.ToList();
            RemoveSubResp();
            _allNameToSign = new Dictionary<string, ConfigVitalSignsModel>();
            foreach (var item in _allSignsList)
            {
                _allNameToSign.Add(item.code, item);
            }

        }

        private void RemoveSubResp()
        {
            int index = -1;
            for (int i = 0; i < _allSignsList.Count; i++)
            {
                if (Constants.PARAM_SUB_RESP.Equals(_allSignsList[i].code))
                {
                    index = i;
                    break;
                }
            }
            if (index != -1)
            {
                _allSignsList.RemoveAt(index);
            }

        }
        private void DividedShowAndNotShowSigns()
        {
            List<ConfigVitalSignsModel> autoShowList = new List<ConfigVitalSignsModel>();
            List<ConfigVitalSignsModel> notAutoShowList = new List<ConfigVitalSignsModel>();
            if (GetPatientLink().VitalSignShow != null)
            {
                autoShowList = GetPatientLink().VitalSignShow.PacuVitalSign;
            }
            else
            {
                autoShowList = GetAutoShow();
            }
            foreach (var item in _allSignsList)
            {
                bool flag = true;
                foreach (var item2 in autoShowList)
                {
                    if (item2.code.Equals(item.code))
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    notAutoShowList.Add(item);
                }

            }
            foreach (var item in autoShowList)
            {
                PacuAutoShowSignsCtrlViewModel.Instance.AutoShowSignsList.Add(item);
                item.PropertyChanged += Item_PropertyChanged;
                if (item.showWay == 0)
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.LineShowSignList.Add(item);
                }
                else
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.TableShowSignList.Add(item);
                    PacuAutoShowSignsCtrlViewModel.Instance.RefershTableVitalIndex();
                }
            }
            foreach (var item in notAutoShowList)
            {
                PacuNoShowSignCtrlViewModel.Instance.NotAutoShowSignsList.Add(item);
            }



        }

        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigVitalSignsModel configVitalSignsModel = sender as ConfigVitalSignsModel;

            if (configVitalSignsModel.code.ToLower().Equals("param_ecg"))
            {
                if (!(configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TABLE))
                {
                    configVitalSignsModel.showWay = Constants.BEDSIDE_SHOW_METHOD_TABLE;
                    MessageBoxCommon.ShowDialog("此参数无法以折线形式显示！", 0);
                }


                return;
            }
            else if (configVitalSignsModel.code.ToLower().Equals("param_i:e"))
            {
                if (!(configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TABLE))
                {
                    configVitalSignsModel.showWay = Constants.BEDSIDE_SHOW_METHOD_TABLE;
                    MessageBoxCommon.ShowDialog("此参数无法以折线形式显示！", 0);
                }

                return;
            }

            if (configVitalSignsModel.code.Equals(Constants.PARAM_CTRL_RESP_CODE) || configVitalSignsModel.code.Equals(Constants.PARAM_RESP_CODE))
            {
                ShowWayChangeForResp(configVitalSignsModel);
                return;
            }
            int showWay = configVitalSignsModel.showWay;
            if (showWay == 1)
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.ChangeDataShowMethod(configVitalSignsModel, Constants.BEDSIDE_SHOW_METHOD_TABLE);
                PacuAutoShowSignsCtrlViewModel.Instance.ChangShowWayFromLine2Table(configVitalSignsModel);
            }
            else
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.ChangeDataShowMethod(configVitalSignsModel, Constants.BEDSIDE_SHOW_METHOD_TREND);
                PacuAutoShowSignsCtrlViewModel.Instance.ChangShowWayFromTable2Line(configVitalSignsModel);
            }
            for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.PacuPage; i++)
            {
                (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
            }

        }


        private void ShowWayChangeForResp(ConfigVitalSignsModel configVitalSignsModel)
        {
            ConfigVitalSignsModel resp = null;
            ConfigVitalSignsModel respCtrl = null;
            foreach (var item in DataManager.Instance.CurrentPatientLink.VitalSignShow.PacuVitalSign)
            {
                if (item.code.Equals(Constants.PARAM_CTRL_RESP_CODE))
                {
                    respCtrl = item;
                }
                if (item.code.Equals(Constants.PARAM_RESP_CODE))
                {
                    resp = item;
                }
            }
            int showWay = configVitalSignsModel.showWay;
            if (showWay == 1)
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.ChangeDataShowMethod(configVitalSignsModel, Constants.BEDSIDE_SHOW_METHOD_TABLE);
                PacuAutoShowSignsCtrlViewModel.Instance.ChangShowWayFromLine2Table(configVitalSignsModel);
            }
            else
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.ChangeDataShowMethod(configVitalSignsModel, Constants.BEDSIDE_SHOW_METHOD_TREND);
                PacuAutoShowSignsCtrlViewModel.Instance.ChangShowWayFromTable2Line(configVitalSignsModel);
            }
            for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.PacuPage; i++)
            {
                (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
            }

            if (configVitalSignsModel.code.Equals(Constants.PARAM_RESP_CODE))
            {
                if (respCtrl == null)
                {
                    return;
                }
                else
                {
                    if (respCtrl.showWay != showWay)
                    {
                        respCtrl.showWay = showWay;
                    }
                }
            }
            if (configVitalSignsModel.code.Equals(Constants.PARAM_CTRL_RESP_CODE))
            {
                if (resp == null)
                {
                    return;
                }
                else
                {
                    if (resp.showWay != showWay)
                    {
                        resp.showWay = showWay;
                    }
                }
            }
            for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.PacuPage; i++)
            {
                (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
            }

        }

        private PatientLinkModel GetPatientLink()
        {
            return DataManager.Instance.CurrentPatientLink == null ? null : DataManager.Instance.CurrentPatientLink;
        }


        #endregion


        #region Public/ProtectedMethods
        public override void Refresh()
        {
            InitData();
        }
        public void ChangSignLeftToRight(ConfigVitalSignsModel configVitalSignsModel)
        {
            bool flag = false;
            if (configVitalSignsModel != null)
            {
                List<ConfigVitalSignsModel> list = PacuAutoShowSignsCtrlViewModel.Instance.AutoShowSignsList.ToList();
                foreach (var item in list)
                {
                    if (item.code.Equals(configVitalSignsModel.code))
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    return;
                }
                PacuNoShowSignCtrlViewModel.Instance.RemoveNotAutoShowListByCode(configVitalSignsModel.code);
                PacuAutoShowSignsCtrlViewModel.Instance.AutoShowSignsList.Add(configVitalSignsModel);
                if (configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TREND)
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.LineShowSignList.Add(configVitalSignsModel);
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISANES))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].RealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISPACU))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                }
                else
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.TableShowSignList.Add(configVitalSignsModel);
                    PacuAutoShowSignsCtrlViewModel.Instance.RefershTableVitalIndex();
                }
                DataManager.Instance.CurrentPatientLink.VitalSignShow.PacuVitalSign.Add(configVitalSignsModel);
                PacuRealTimeSignMainShowCtrlViewModel.Instance.AddConfigVitalSign(configVitalSignsModel);
                PacuRealTimeSignLeftCtrlViewModel.Instance.AddConfigVitalSignName(configVitalSignsModel);

                configVitalSignsModel.PropertyChanged += Item_PropertyChanged;
                PatientLinkService.Instance.Update(GetPatientLink());
            }

        }
        public void ChangSignRightToLeft(ConfigVitalSignsModel configVitalSignsModel)
        {

            if (configVitalSignsModel != null)
            {
                PacuAutoShowSignsCtrlViewModel.Instance.RemoveAutoShowListByCode(configVitalSignsModel.code);
                if (configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TREND)
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.RemoveLineShowListByCode(configVitalSignsModel.code);
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISANES))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].RealTimeItemCtrlViewModel.RemoveVitalItemBy(configVitalSignsModel.code);
                    //    }
                    //}
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISPACU))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                }
                else
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.RemoveTableShowListByCode(configVitalSignsModel.code);
                    PacuAutoShowSignsCtrlViewModel.Instance.RefershTableVitalIndex();
                }
                PacuNoShowSignCtrlViewModel.Instance.NotAutoShowSignsList.Insert(0, configVitalSignsModel);
                DataManager.Instance.ReomveVitalSignByCode(configVitalSignsModel.code);
                PacuRealTimeSignMainShowCtrlViewModel.Instance.RemoveConfigVitalSign(configVitalSignsModel);
                PacuRealTimeSignLeftCtrlViewModel.Instance.RemoveConfigVitalSignName(configVitalSignsModel);

                configVitalSignsModel.PropertyChanged -= Item_PropertyChanged;
                PatientLinkService.Instance.Update(GetPatientLink());
            }
        }
        public void ChangSignLeftToRightForResp(string code)
        {
            ConfigVitalSignsModel configVitalSignsModel = DataManager.Instance.Code2signs[code];
            if (configVitalSignsModel != null)
            {
                PacuNoShowSignCtrlViewModel.Instance.RemoveNotAutoShowListByCode(configVitalSignsModel.code);
                PacuAutoShowSignsCtrlViewModel.Instance.AutoShowSignsList.Add(configVitalSignsModel);
                if (configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TREND)
                {
                   PacuAutoShowSignsCtrlViewModel.Instance.LineShowSignList.Add(configVitalSignsModel);
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISANES))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].RealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISPACU))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                }
                else
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.TableShowSignList.Add(configVitalSignsModel);
                    PacuAutoShowSignsCtrlViewModel.Instance.RefershTableVitalIndex();
                }
                DataManager.Instance.CurrentPatientLink.VitalSignShow.PacuVitalSign.Add(configVitalSignsModel);

                PacuRealTimeSignMainShowCtrlViewModel.Instance.AddConfigVitalSign(configVitalSignsModel);
                PacuRealTimeSignLeftCtrlViewModel.Instance.AddConfigVitalSignName(configVitalSignsModel);
                configVitalSignsModel.PropertyChanged += Item_PropertyChanged;
                PatientLinkService.Instance.Update(GetPatientLink());
            }
        }
        public void ChangSignRightToLeftForResp(string code)
        {
            ConfigVitalSignsModel configVitalSignsModel = DataManager.Instance.Code2signs[code];
            if (configVitalSignsModel != null)
            {
                PacuAutoShowSignsCtrlViewModel.Instance.RemoveAutoShowListByCode(configVitalSignsModel.code);
                if (configVitalSignsModel.showWay == Constants.BEDSIDE_SHOW_METHOD_TREND)
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.RemoveLineShowListByCode(configVitalSignsModel.code);
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISANES))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].RealTimeItemCtrlViewModel.RemoveVitalItemBy(configVitalSignsModel.code);
                    //    }
                    //}
                    //if (DataManager.Instance.IsInAorP.Equals(Constants.ISPACU))
                    //{
                    //    for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
                    //    {
                    //        (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[i].PacuRealTimeItemCtrlViewModel.ShowWayChanged(configVitalSignsModel);
                    //    }
                    //}
                }
                else
                {
                    PacuAutoShowSignsCtrlViewModel.Instance.RemoveTableShowListByCode(configVitalSignsModel.code);
                    PacuAutoShowSignsCtrlViewModel.Instance.RefershTableVitalIndex();
                }
                PacuNoShowSignCtrlViewModel.Instance.NotAutoShowSignsList.Insert(0, configVitalSignsModel);
                DataManager.Instance.ReomveVitalSignByCode(configVitalSignsModel.code);

                PacuRealTimeSignMainShowCtrlViewModel.Instance.RemoveConfigVitalSign(configVitalSignsModel);
                PacuRealTimeSignLeftCtrlViewModel.Instance.RemoveConfigVitalSignName(configVitalSignsModel);

                configVitalSignsModel.PropertyChanged -= Item_PropertyChanged;
                PatientLinkService.Instance.Update(GetPatientLink());
            }

        }
        public List<ConfigVitalSignsModel> GetAutoShow()
        {
            List<ConfigVitalSignsModel> configParams = new List<ConfigVitalSignsModel>();
            Dictionary<string, ConfigVitalSignsModel> code2signs = DataManager.Instance.Code2signs;
            configParams.Add(code2signs["param_ECG"]);
            configParams.Add(code2signs["param_spo2"]);
            configParams.Add(code2signs["param_ETCO2"]);
            configParams.Add(code2signs["param_T"]);
            configParams.Add(code2signs["param_HR"]);
            configParams.Add(code2signs["param_resp"]);
            configParams.Add(code2signs["param_nibp_s"]);
            configParams.Add(code2signs["param_nibp_d"]);
            return configParams;
        }



        #endregion


        #region EventHandlers


        #endregion

    }
}
